package design.onekey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import design.onekey.base.Constants;
import design.onekey.base.StatusCode;
import design.onekey.model.dto.UserInfoDto;
import design.onekey.exception.BizException;
import design.onekey.model.SysFunction;
import design.onekey.model.UserAccount;
import design.onekey.model.UserAccountOrg;
import design.onekey.model.UserRole;
import design.onekey.service.*;
import design.onekey.util.JWTUtil;
import design.onekey.util.Md5Util;
import design.onekey.util.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: LoginServiceImpl
 * @Description: 登录用服务实现
 * @author blcloak
 * @date 2020-06-05 04:44:44
*/
@Service
@Slf4j
public class LoginServiceImpl implements ILoginService {

	/**
	 * @Fields IUserAccountService 服务注入
	 * @author blcloak
	 * @date 2020-06-05 04:47:14
	 */
	@Autowired
	private IUserAccountService iUserAccountService;

	/**
	 * @Fields iUserRoleService 服务注入
	 * @author blcloak
	 * @date 2020-06-05 05:26:56
	 */
	@Autowired
	private IUserRoleService iUserRoleService;

	/**
	 * @Fields iUserAccountOrgService 服务注入
	 * @author blcloak
	 * @date 2020-06-05 05:27:07
	 */
	@Autowired
	private IUserAccountOrgService iUserAccountOrgService;

	/**
	 * @Fields iSysFunctionService 服务注入
	 * @author blcloak
	 * @date 2020-06-05 05:50:51
	 */
	@Autowired
	private ISysFunctionService iSysFunctionService;

	/**
	 * @Title: doLogin
	 * @Description: 平台用户登录
	 * @param userAccount
	 * @return
	 * @author blcloak
	 * @date 2020-06-05 04:46:23
	 */
	@Override
	public UserInfoDto doLogin(UserAccount userAccount) {
		UserInfoDto result = new UserInfoDto();
		//根据用户输入的账号查询账号信息
		QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<UserAccount>();
		queryWrapper.eq("account_name", userAccount.getAccountName());
		queryWrapper.eq("del_flag", 1);
		UserAccount user = iUserAccountService.getOne(queryWrapper);
		if (user == null) {//用户不存在
			throw new BizException(StatusCode.LOGIN_FAILURE, MessageUtil.getValue("error.user.notexist"));
		}

		//对比密码
		if (!Md5Util.generateMd5(userAccount.getUserPwd()).equals(user.getUserPwd())) {
			throw new BizException(StatusCode.LOGIN_FAILURE, MessageUtil.getValue("error.login"));
		}
		result.setUserId(user.getId());
		result.setUserAccount(user.getAccountName());
		result.setUserName(user.getUserName());
		result.setIsAdmin(user.getIsAdmin());
		//非超级管理员角色需获取角色信息和组织信息
		if (1 != user.getIsAdmin()) {
			//获取用户角色信息
			UserRole userRole = iUserRoleService.getUserRole(user.getId());
			if (userRole == null) {
				throw new BizException(StatusCode.LOGIN_FAILURE, MessageUtil.getValue("error.norole"));
			}
			result.setRoleId(userRole.getId());
			result.setRoleName(userRole.getUserRoleName());
			result.setRoleCode(userRole.getUserRoleCode());
			//获取组织信息
			QueryWrapper<UserAccountOrg> userAccountOrgQueryWrapper = new QueryWrapper<>();
			userAccountOrgQueryWrapper.eq("user_account_id", user.getId());
			userAccountOrgQueryWrapper.eq("del_flag", 1);
			userAccountOrgQueryWrapper.last(Constants.LIMITSQL);
			UserAccountOrg userAccountOrg = this.iUserAccountOrgService.getOne(userAccountOrgQueryWrapper);
			if(userAccountOrg != null)
			{
				result.setOrgId(userAccountOrg.getOrgId());
			}
			//获取角色对应的功能权限
			List<SysFunction> roleFunctions = iSysFunctionService.getFunctionsByRole(userRole.getId());
			List<String> functions = new ArrayList<String>();
			if (roleFunctions != null && roleFunctions.size() > 0) {
				for (SysFunction sysFunction : roleFunctions) {
					functions.add(sysFunction.getFunctionCode());
				}
			}
			result.setFunctions(functions);
		}else {
			List<String> functions = new ArrayList<String>();
			//超级用户获取所有的权限
			QueryWrapper<SysFunction> functionQueryWrapper = new QueryWrapper<SysFunction>();
			functionQueryWrapper.eq("del_flag", 1);
			List<SysFunction> roleFunctions = iSysFunctionService.list(functionQueryWrapper);
			if (roleFunctions != null && roleFunctions.size() > 0) {
				for (SysFunction sysFunction : roleFunctions) {
					functions.add(sysFunction.getFunctionCode());
				}
			}
			result.setFunctions(functions);
		}
		//生成token
		String token = JWTUtil.sign(result, user.getUserPwd());
		result.setToken(token);
		return result;
	}

}
